package com.lovestudying.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.lovestudying.dao.CourseDao;
import com.lovestudying.dao.TabDao;
import com.lovestudying.dao.Impl.CourseDaoImpl;
import com.lovestudying.dao.Impl.TabDaoImpl;
import com.lovestudying.entity.Course;
import com.lovestudying.entity.Tab;
import com.lovestudying.service.CourseService;
import com.lovestudying.util.DBUtil_Pro;
import com.lovestudying.util.PageInfo;

public class CourseServiceImpl implements CourseService {

	private CourseDao courseDao = new CourseDaoImpl();
	private TabDao TabDao = new TabDaoImpl();

	@Override
	public PageInfo<Course> getCourseInformation(Map<String, String> params) {
		// 模糊业务查询
		params.put("courseTitle", "%" + params.get("courseTitle") + "%");
		return courseDao.findCourseInformation(params);
	}

	@Override
	public Course getCourseInformationById(String courseId) {

		Map<String, Object> Where_Param = new HashMap<String, Object>();
		Where_Param.put("course.course_id", courseId);
		Course course = courseDao.findCourseUniTeacher(Where_Param, null, null, null).getData().get(0);
		return course;
	}

	@Override
	public List<Course> getCourseListByCondition(String teacherKey) {
		// TODO 自动生成的方法存根
		Map<String, Object> Where_Param = new HashMap<String, Object>();
		Where_Param.put("teacher.teacher_name", teacherKey);

		return courseDao.findCourseUniTeacher(Where_Param, null, null, null).getData();
	}

	@Override
	public Integer DeleteCourseById(String courseId) {
		Map<String, Object> map = new HashMap<>();
		map.put("course_id", courseId);
		return courseDao.DeleteCourse(map);
	}

	@Override
	public List<Course> getIntrestedCourseByTab(String[] tabs) {
		// TODO 自动生成的方法存根
		List<String> tabsList = Arrays.asList(tabs);
		List<Course> findCourseUniTab = courseDao.findCourseUniTab(tabsList);

		return DBUtil_Pro.GetRandomInfoList(findCourseUniTab, 3);
	}

	@Override
	public Boolean updateCourseById(String updateCourseId, String updateCourseTitle, Integer payType, String introdu,
			Double updateCoursePrice) {
		Map<String, Object> Set_Param = new HashMap<>();
		Map<String, Object> Where_Param = new HashMap<>();
		Set_Param.put("pay_type", payType);
		Set_Param.put("course_title", updateCourseTitle);
		Set_Param.put("course_introdu", introdu);
		Set_Param.put("course_price", updateCoursePrice);
		Where_Param.put("course_id", updateCourseId);

		if (courseDao.UpdateCourse(Set_Param, Where_Param) != 0) {
			return true;
		} else {
			return false;
		}

	}

	@Override
	public Boolean addCourse(String courseUuid, String addCourseTitle, Double addCoursePrice, Integer payType,
			String addIntrodu, String fileName, String teacherId, List<String> Tabs) {
		Map<String, Object> Value_Param = new HashMap<>();
		Value_Param.put("course_id", courseUuid);
		Value_Param.put("course_title", addCourseTitle);
		Value_Param.put("course_price", addCoursePrice);
		Value_Param.put("pay_type", payType);
		Value_Param.put("course_introdu", addIntrodu);
		if (fileName != null) {
			Value_Param.put("course_pic", fileName);
		}
		if (teacherId != null && !"".equals(teacherId)) {
			Value_Param.put("teacher_id", teacherId);
		}
		Value_Param.put("course_create_time", DBUtil_Pro.GetNowTime());
		if (Tabs.size() > 0) {
			for (String Tab : Tabs) {
				Map<String, Object> valueForTab = new HashMap<>();
				valueForTab.put("course_id", courseUuid);
				valueForTab.put("course_tab", Tab);
				TabDao.InsertTab(valueForTab);
			}
		}

		if (courseDao.InsertCourse(Value_Param) == 1) {
			return true;
		} else {
			return false;
		}

	}

	@Override
	public PageInfo<Course> getCourseInformationByTab(Map<String, String> params) {
		params.put("courseTitle", "%" + params.get("courseTitle") + "%");
		return courseDao.findCourseInformationByKeys(params);
	}

}
